package org.tio.sitexxx.web.server.controller.minip;


import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.http.common.HttpRequest;
import com.alibaba.fastjson.JSON;
import org.tio.http.server.annotation.RequestPath;
import org.tio.sitexxx.service.model.main.Member;
import org.tio.sitexxx.service.service.qj.MemberService;
import org.tio.sitexxx.service.service.qj.UserService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@RequestPath(value = "/setting")
public class SettingController  {
    private static Logger log = LoggerFactory.getLogger(SettingController.class);
    public SettingController(){

    }

    /**
     * 用户授权微信登陆后填写姓名和电话号码
     * 用户登录进去之后修改个人信息  也是用这个接口
     * @param request
     * @return
     */
    @RequestPath(value = "/modify_record")
    public  Object modify_record(HttpRequest request){
        Map<String,Object>requestmap= JSON.parseObject(request.getBodyString());
//        System.out.println("modify_record:"+JSON.toJSONString(requestmap));
        Map<String, Object> query = new HashMap<>();
        Map<String, Object> resultMap = new HashMap<>();
        try{
            String openid=requestmap.get("openid").toString();//用户第一次登陆后填写手机姓名
            String name=requestmap.get("name").toString();
            String mobile=requestmap.get("mobile").toString();
            if(openid==null||openid.length()<20){
                resultMap.put("ok",1);
                System.out.println();
                return resultMap;
            }
            query.put("openid", requestmap.get("openid"));
            final Member  Member = MemberService.me.getMember(query);
            int flag =0;
            if(Member==null){
//            Member = new Member();//member正常情况下不可能为null
                flag=1;
            }
            Member.setName(name);
            Member.setProfession((String)requestmap.get("profession"));//用户登录进去之后修改个人信息  共用一个接口
            Member.setGender((String)requestmap.get("sex"));
            Member.setAgeScope((String)requestmap.get("age_scope"));
            Member.setMobile(mobile);
            if(flag==0){
                Db.tx(new IAtom() {
                    @Override
                    public boolean run() throws SQLException {
                        MemberService.me.update(Member);
                        UserService.me.update(openid,name);
                        return true;
                    }
                });
                resultMap.put("ok",0);
            }
            else {
                resultMap.put("ok",1);
                System.out.println("用户不存在");
            }
        }catch (NullPointerException e){
            log.error(e.toString(),e);
            e.printStackTrace();
            resultMap.put("ok","参数错误");
        }catch (Exception e){
            log.error(e.toString(),e);
            e.printStackTrace();
            resultMap.put("ok",1);
        }
        return resultMap;
    }

}
